package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.MessageTypeEnum;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.framework.config.mq.dto.MessageOrdersMessageDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.Courier;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.UserMessage;
import com.qinggeng.project.system.service.ICourierService;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.IUserMessageService;
import com.qinggeng.project.system.service.MessagePushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.qinggeng.common.enums.OrdersStatusCodeEnum.getOrdersStatusCode;


@Slf4j
@Component
public class OrdersMessageJPushHandleImpl implements MessageHandle {

    @Autowired
    private IUserMessageService messageService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private MessagePushService messagePushService;
    @Autowired
    private ICourierService courierService;

    @Override
    public void handle(String msg) {

        System.out.println("解析订单消息推送handle "+msg);

        MessageOrdersMessageDTO ordersMessageDTO= JSON.parseObject(msg,MessageOrdersMessageDTO.class);

        Orders orders=ordersService.selectOrdersByOrdersNo(ordersMessageDTO.getOrdersNo());
        if(null==orders){
            throw new CustomException("订单不存在");
        }

        try{
            //发送站内信息
            this.insertUserMessage(ordersMessageDTO);

            //极光推送消息
            if(UserTypeEnum.MEMBER.getCode().equals(ordersMessageDTO.getUserType())){
                messagePushService.pushMemberOrdersMessage(orders,ordersMessageDTO.getMessage());
            }
            if(UserTypeEnum.COURIER.getCode().equals(ordersMessageDTO.getUserType())){
                messagePushService.pushCourierOrdersMessage(orders,ordersMessageDTO.getMessage());
            }

        }catch (Exception e){
            log.error("錯誤"+e.getMessage());
        }
    }

    /**
     * 发送站内信息
     * @param messageDTO
     */
    private void insertUserMessage(MessageOrdersMessageDTO messageDTO){
        Orders orders=ordersService.selectOrdersByOrdersNo(messageDTO.getOrdersNo());
        if(null==orders){
            return;
        }
        UserMessage userMessage=new UserMessage();
        if(UserTypeEnum.MEMBER.getCode().equals(messageDTO.getUserType())){
            if(null==orders.getUserId()){
                return;
            }
            userMessage.setUserId(orders.getUserId());
        }
        if(UserTypeEnum.COURIER.getCode().equals(messageDTO.getUserType())){
            Courier courier=courierService.getCourierByOrdersId(orders.getOrdersId());
            if(null==courier||null==courier.getUserId()){
                return;
            }
            userMessage.setUserId(courier.getUserId());
        }
        userMessage.setMessageType(MessageTypeEnum.订单消息.getValue());
        userMessage.setFkId(orders.getOrdersId());
        userMessage.setFkMark(messageDTO.getMark());
        userMessage.setFkContent(messageDTO.getMessage());
        userMessage.setStatusCode(getOrdersStatusCode(orders).getCode());
        userMessage.setCreateTime(DateUtils.getNowDate());
        int rows=messageService.insertUserMessage(userMessage);
        if(rows<1){
            log.error("保存消息记录失败");
        }
    }


}
